package com.hmkj.web.config;

import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.api.ufx.call.TransactionCall;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.constant.State;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.risk.RiskParam;
import com.hmkj.core.po.system.Configure;
import com.hmkj.core.po.system.MessageTemplate;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.commo.CommoService;
import com.hmkj.core.service.risk.RiskParamService;
import com.hmkj.core.service.system.ConfigureService;
import com.hmkj.core.service.system.MessageTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化数据
 */
@Slf4j
@Component
public class Init implements CommandLineRunner {

    @Override
    public void run(String... strings) throws Exception {
        log.info("初始化数据...");
        MessageTemplateService messageTemplateService = SpringContextUtils.getBean(MessageTemplateService.class);
        ConfigureService configureService = SpringContextUtils.getBean(ConfigureService.class);
        CommoService commoService = SpringContextUtils.getBean(CommoService.class);
        RiskParamService riskParamService = SpringContextUtils.getBean(RiskParamService.class);
        RedisService redisService = SpringContextUtils.getBean(RedisService.class);


        /** 缓存系统参数 */
        setWebConfigToRedis(configureService.select(new Configure(t -> t.setStatus(State.YES))), redisService);
        log.info("加载系统参数成功！");

        /** 缓存消息模板 */
        setMessageTempToRedis(messageTemplateService.select(new MessageTemplate()), redisService);
        log.info("加载消息模板成功！");

        /** 缓存期货品种 */
        setCommoToRedis(commoService.select(new Commo(t -> {
            t.setDelFlag(Commo.DELFLAG.NORMAL.code);
            t.setStatus(Commo.STATUS.T1.code);
        })), redisService);
        log.info("加载期货品种成功！");

        /** 缓存风控参数 */
        setRiskParamsToRedis(riskParamService.select(new RiskParam()), redisService);
        log.info("加载风控参数成功！");

        /** 初始化交易服务器连接 */
        TransactionCall.getInstance().initT2();
        log.info("初始化交易服务器连接成功！");

    }

    private void setRiskParamsToRedis(List<RiskParam> list, RedisService redisService) {
        for (RiskParam param : list) {
            redisService.put(CacheID.RISK_PREFIX + param.getNid() + ":" + param.getCommoId(), param.getValue());
        }
    }

    private void setCommoToRedis(List<Commo> list, RedisService redisService) {
        if (CollectionUtils.isNotEmpty(list)) {
            redisService.put(CacheID.COMMO_LIST, list);
            for (Commo commo : list) {
                redisService.put(CacheID.COMMO_ID_PREFIX + commo.getId(), commo);
                redisService.put(CacheID.COMMO_CODE_PREFIX + commo.getCode(), commo);
            }
        }
    }


    private static void setWebConfigToRedis(List<Configure> list, RedisService redisService) {
        // 更新缓存系统参数
        for (Configure config : list) {
            log.debug("缓存参数:key=" + CacheID.CONFIG_PREFIX + config.getNid() + ",val=" + config.getValue());
            redisService.put(CacheID.CONFIG_PREFIX + config.getNid(), config.getValue());
        }
    }

    private static void setMessageTempToRedis(List<MessageTemplate> list, RedisService redisService) {
        // 更新缓存系统参数
        for (MessageTemplate template : list) {
            log.debug("缓存参数:key=" + CacheID.TEMPLATE_MESSAGE_PREFIX + template.getNid() + ",val=" + template);
            redisService.put(CacheID.TEMPLATE_MESSAGE_PREFIX + template.getNid(), template);
        }
    }


}
